package com.ptadmin.report.executor.processor;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.ptadmin.report.converter.ReportConverterBase;
import com.ptadmin.report.converter.ReportConverterTabDelimited;
import com.ptadmin.report.converter.ReportDecorator;
import com.ptadmin.report.output.FlatFileOutput;
import com.ptadmin.report.output.MongoDbOutput;
import com.ptadmin.report.util.Filename;

public class DefaultPGProcessor {
	
	private ReportDecorator decorator;
	private ReportConverterBase jsonConverter;
	JsonParser parser = new JsonParser();
	
	public void process(ProcessReportsParameter p) {
		this.decorator = new ReportDecorator();
		this.decorator.setSellerId(p.getMerchantId());
		this.jsonConverter = new ReportConverterTabDelimited();
		this.jsonConverter.readFrom(p.getReportFilePath());
		String reportDataJson = this.jsonConverter.toJson();
		JsonElement reportData = parser.parse(reportDataJson);

		this.decorator.setReportData(reportData);
		
		// Output to mongodb
		MongoDbOutput mo = new MongoDbOutput(decorator);
		mo.output(p.getReportName());
		
		// Output to flatfile
		FlatFileOutput fo = new FlatFileOutput(decorator);

		Filename filename = new Filename(p.getReportFilePath(), '/', '.');
				
		fo.output(filename.filename() + ".json");
	}
}
